$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'order/orderitem/list',
        datatype: "json",
        colModel: [			
			{ label: 'itemId', name: 'itemId', index: 'item_id', width: 50, key: true },
			{ label: '订单ID', name: 'orderId', index: 'order_id', width: 80 }, 			
			{ label: '货品ID', name: 'productId', index: 'product_id', width: 80 }, 			
			{ label: '商品ID', name: 'goodsId', index: 'goods_id', width: 80 }, 			
			{ label: '是否加急', name: 'urgent', index: 'urgent', width: 80 }, 			
			{ label: '是否可评论', name: 'comment', index: 'comment', width: 80 }, 			
			{ label: '商品图片地址', name: 'goodsImgUrl', index: 'goods_img_url', width: 80 }, 			
			{ label: '明细商品的名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '明细商品的销售价(购入价)', name: 'price', index: 'price', width: 80 }, 			
			{ label: '明细商品总额', name: 'amount', index: 'amount', width: 80 }, 			
			{ label: '分摊后价格', name: 'sharePrice', index: 'share_price', width: 80 }, 			
			{ label: '明细商品重量', name: 'weight', index: 'weight', width: 80 }, 			
			{ label: '明细商品购买数量', name: 'nums', index: 'nums', width: 80 }, 			
			{ label: '商品单位', name: 'unit', index: 'unit', width: 80 }, 			
			{ label: '明细商品的规格属性', name: 'addon', index: 'addon', width: 80 }, 			
			{ label: '明细商品类型 product:商品,pkg:捆绑商品,gift:赠品商品,adjunct:配件商品', name: 'itemType', index: 'item_type', width: 80 }, 			
			{ label: '工艺IDs', name: 'craftsIds', index: 'crafts_ids', width: 80 }, 			
			{ label: '工艺名', name: 'craftsNames', index: 'crafts_names', width: 80 }, 			
			{ label: 'normal：普通商品print：印刷商品design_service：设计商品quote：报价商品paper：大纸商品fold：折页template：索样商品', name: 'goodsType', index: 'goods_type', width: 80 },
			{ label: '明细附件对象', name: 'itemTypeObj', index: 'item_type_obj', width: 80 }, 			
			{ label: '明细附件对象IDs', name: 'itemTypeObjId', index: 'item_type_obj_id', width: 80 }, 			
			{ label: '大纸购买类型 ton:吨，ream:令', name: 'paperUnit', index: 'paper_unit', width: 80 }, 			
			{ label: '大纸购买规格：长，宽', name: 'paperSize', index: 'paper_size', width: 80 }, 			
			{ label: '明细商品积分', name: 'score', index: 'score', width: 80 }, 			
			{ label: '审核状态', name: 'approvalStatus', index: 'approval_status', width: 80 }, 			
			{ label: '明细商品发货数量', name: 'sendnum', index: 'sendnum', width: 80 }, 			
			{ label: '门店货品ID', name: 'merchantsProductId', index: 'merchants_product_id', width: 80 }, 			
			{ label: '门店货品 份数', name: 'merchantsCopy', index: 'merchants_copy', width: 80 }, 			
			{ label: '伪删除标记（0正常 1删除）', name: 'itemDel', index: 'item_del', width: 80 }, 			
			{ label: '模板id', name: 'goodsTempletId', index: 'goods_templet_id', width: 80 }, 			
			{ label: '关联商品ID', name: 'origItemId', index: 'orig_item_id', width: 80 }, 			
			{ label: '报价订单算价过程', name: 'calculationPriceProcess', index: 'calculation_price_process', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		orderItem: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.orderItem = {};
		},
		update: function (event) {
			var itemId = getSelectedRow();
			if(itemId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(itemId)
		},
		saveOrUpdate: function (event) {
			var url = vm.orderItem.itemId == null ? "order/orderitem/save" : "order/orderitem/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.orderItem),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var itemIds = getSelectedRows();
			if(itemIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "order/orderitem/delete",
                    contentType: "application/json",
				    data: JSON.stringify(itemIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(itemId){
			$.get(baseURL + "order/orderitem/info/"+itemId, function(r){
                vm.orderItem = r.orderItem;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		}
	}
});